package com.piece.core.log.service;

import com.piece.core.log.client.LoginLogService;
import com.piece.core.log.client.dto.LoginLogDTO;
import com.piece.core.log.enums.LoginLogType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 登录日志实现类
 */
@Slf4j
@Component("login_log_service_logger")
@ConditionalOnProperty(name = "spring.log.type", havingValue = "logger")
public class LoginLogServiceLogger implements LoginLogService {

    /**
     * 日志格式：{时间}|{应用名}|{类型}|{租户id}|{登录用户id}|{登录用户名}|{访问ip}|{操作城市}|{操作地点}|{错误消息}
     */
    private static final String MSG_PATTERN = "{}|{}|{}|{}|{}|{}|{}|{}|{}|{}";

    @Async
    @Override
    public LoginLogDTO login(LoginLogDTO loginLog) {
        log.info(MSG_PATTERN
                , "时间=" + LocalDateTime.ofInstant(loginLog.getOperaTime().toInstant(), ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
                , "应用名=" + loginLog.getApplicationName()
                , "类型=" + LoginLogType.LOGIN.name()
                , "租户ID=" + loginLog.getClientId()
                , "登录用户ID=" + loginLog.getUserId()
                , "登录用户名=" + loginLog.getUserName()
                , "访问ip=" + loginLog.getIp()
                , "操作城市=" + loginLog.getCity()
                , "操作地点=" + loginLog.getArea()
                , "错误消息=" + ("1".equals(loginLog.getStatus()) ? loginLog.getDesc() : ""));

        return loginLog;
    }

    @Async
    @Override
    public LoginLogDTO logout(LoginLogDTO loginLog) {
        log.info(MSG_PATTERN
                , "时间=" + LocalDateTime.ofInstant(loginLog.getOperaTime().toInstant(), ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
                , "应用名=" + loginLog.getApplicationName()
                , "类型=" + LoginLogType.LOGOUT.name()
                , "租户ID=" + loginLog.getClientId()
                , "登录用户ID=" + loginLog.getUserId()
                , "登录用户名=" + loginLog.getUserName()
                , "访问ip=" + loginLog.getIp()
                , "操作城市=" + loginLog.getCity()
                , "操作地点=" + loginLog.getArea()
                , "错误消息=" + ("1".equals(loginLog.getStatus()) ? loginLog.getDesc() : ""));

        return loginLog;
    }

    @Async
    @Override
    public LoginLogDTO force(LoginLogDTO loginLog) {
        log.info(MSG_PATTERN
                , "时间=" + LocalDateTime.ofInstant(loginLog.getOperaTime().toInstant(), ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
                , "应用名=" + loginLog.getApplicationName()
                , "类型=" + LoginLogType.FORCE.name()
                , "租户ID=" + loginLog.getClientId()
                , "登录用户名=" + loginLog.getUserName()
                , "操作用户名=" + loginLog.getOperaName()
                , "访问ip=" + loginLog.getIp()
                , "操作城市=" + loginLog.getCity()
                , "操作地点=" + loginLog.getArea()
                , "错误消息=" + ("1".equals(loginLog.getStatus()) ? loginLog.getDesc() : ""));

        return loginLog;
    }

    public void deleteByClientId(String clientId) {
    }

    public void clear() {
    }

    public List<LoginLogDTO> findByCondition(Map<String, Object> param) {
        return Collections.emptyList();
    }
}
